/**
 * Copyright 2011 Joao Miguel Pereira
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package eu.jpereira.trainings.designpatterns.creational.builder.xml;

import java.util.Iterator;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SaleEntry;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SoldItem;

/**
 * This class is for demonstration purpose only!!
 * 
 * @author jpereira
 * 
 */
public class XMLReportBody implements ReportBody {

        private StringBuilder stringBuilder = new StringBuilder();

        /*
         * (non-Javadoc)
         * 
         * @see eu.jpereira.trainings.designpatterns.creational.builder.ReportBody#
         * getAsString()
         */
        @Override
        public Object getAsString() {

                return stringBuilder.toString();
        }

        public void addContent(Object content) {
                this.stringBuilder.append(content);
        }

        @Override
        public void content(ReportBody rb, SaleEntry saleEntry) {

                rb.addContent("<sale><customer><name>");
                rb.addContent(saleEntry.getCustomer().getName());
                rb.addContent("</name><phone>");
                rb.addContent(saleEntry.getCustomer().getPhone());
                rb.addContent("</phone></customer>");
                rb.addContent("<items>");

        }

        @Override
        public void iterate(ReportBody rb, SaleEntry saleEntry) {

                Iterator<SoldItem> it = saleEntry.getSoldItems().iterator();

                while (it.hasNext()) {

                        SoldItem soldEntry = it.next();
                        rb.addContent("<item><name>");
                        rb.addContent(soldEntry.getName());
                        rb.addContent("</name><quantity>");
                        rb.addContent(soldEntry.getQuantity());
                        rb.addContent("</quantity><price>");
                        rb.addContent(soldEntry.getUnitPrice());
                        rb.addContent("</price></item>");
                }

                rb.addContent("</items></sale>");

        }

}